package org.helgoboss.obr_commandlet_maven_plugin;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.helgoboss.jvm_commandlet_client.JvmCommandletClient;

/**
 * Goal to install or refresh a certain OBR resource in a running OSGi container. Uses the Commandlet Platform.
 * 
 * @goal refresh-resource
 */
public class ObrCommandletMavenPlugin extends AbstractMojo {

	/**
	 * @parameter expression="${project}"
	 */
	MavenProject project;
	
	/**
	 * @parameter default-value="${project.basedir}"
	 */
	File workingDirectory;

	public void execute() throws MojoExecutionException, MojoFailureException {
		JvmCommandletClient client = new JvmCommandletClient();
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		byte[] emptyInput = new byte[0];
		ByteArrayInputStream input = new ByteArrayInputStream(emptyInput);
		String[] argumentArray = new String[] {"refresh", project.getGroupId() + "." + project.getArtifactId()};
		client.executeCommandlet("org.helgoboss.obr", workingDirectory, argumentArray, input, output);
		getLog().info(output.toString());
		
	}
}
